import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login' // 自动重定向到登录页
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    // meta: {
    //   title: '主页'
    // }
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 设置路由守卫动态修改标题
router.beforeEach((to, from, next) => {
  const defaultTitle = process.env.VUE_APP_TITLE
  if (to.meta.title) {
    document.title = to.meta.title as string
    // document.title = `${to.meta.title} | ${defaultTitle}`
  } else {
    document.title = defaultTitle
  }
  next()
})

export default router
